
package com.wyzz.hr.action;

import java.io.PrintStream;
import java.util.*;
import org.apache.commons.beanutils.BeanComparator;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IIodefBo;
import com.wyzz.hr.bo.IOutmatchBO;
import com.wyzz.hr.bo.IOutmatchBasicBO;
import com.wyzz.hr.bo.IOutmatchModelBO;
import com.wyzz.hr.pojo.Iodef;
import com.wyzz.hr.pojo.Outmatch;
import com.wyzz.hr.pojo.OutmatchModel;
import com.wyzz.hr.util.output.FormatToString;

public class OutmatchModelList extends BaseAction {

	public OutmatchModelList() {
	}

	public String execute() throws Exception {
		if (!setIodef(ioId)) {
			return "input";
		} else {
			IOutmatchModelBO outmatchModelBO = (IOutmatchModelBO) getBean("outmatchModelBO");
			ommList = outmatchModelBO.getListByIodef(iodef);
			return "success";
		}
	}

	private boolean setIodef(String ioId) {
		if (ioId == null || ioId.length() < 1)
			return false;
		IIodefBo iodefBo = (IIodefBo) getBean("iodefbo");
		iodef = iodefBo.loadObject(ioId);
		if (iodef == null) {
			System.out.println("==========ioId is illeagle");
			return false;
		} else {
			return true;
		}
	}

	public OutmatchModel loadOutmatchModel(String ioId, String ommId)
			throws Exception {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("iodef", "view"))) {
			System.out.println("==========error: has no auth");
			return null;
		}
		if (!setIodef(ioId))
			return null;
		IOutmatchModelBO outmatchModelBO = (IOutmatchModelBO) getBean("outmatchModelBO");
		OutmatchModel outmatchModel = null;
		if (ommId != null && ommId.length() > 0) {
			outmatchModel = outmatchModelBO.getObject(iodef, ommId);
			if (outmatchModel == null) {
				System.out.println("==========ommId is illeagle");
				return null;
			}
			IOutmatchBO outmatchBO = (IOutmatchBO) getBean("outmatchBO");
			List omList = outmatchBO.getFullOutmatchList(outmatchModel);
			outmatchBO.initOutmatchMaps(omList);
			outmatchModel.setOmList(omList);
		} else {
			if ("error".equalsIgnoreCase(DWRUtil.checkAuth("iodef", "edit"))) {
				System.out.println("==========error: has no auth");
				return null;
			}
			outmatchModel = new OutmatchModel();
			IOutmatchBasicBO outmatchBasicBO = (IOutmatchBasicBO) getBean("outmatchBasicBO");
			outmatchModel.setOmList(outmatchBasicBO.createOutmatch(iodef));
			outmatchModel.setOmmNoTitle(Integer.valueOf(0));
			outmatchModel.setOmmRowHeight(Integer.valueOf(25));
		}
		outmatchModel.setAllMaps();
		outmatchModel.setOmmIo(iodef);
		return outmatchModel;
	}

	public String saveOrUpdateOutmatchModel(OutmatchModel outmatchModel,
			List omList) throws Exception {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("iodef", "edit")))
			return "error: has no auth";
		if (outmatchModel == null)
			return "outmatchModel is illeagle";
		if (outmatchModel.getOmmId() == null
				|| outmatchModel.getOmmId().length() < 1)
			outmatchModel.setOmmId(null);
		if (omList == null)
			omList = new ArrayList();
		OutmatchModel outmatchModelDB = loadOutmatchModel(outmatchModel
				.getOmmIo().getIoId(), outmatchModel.getOmmId());
		if (outmatchModelDB == null)
			return "outmatchModel is illeagle";
		outmatchModelDB.setOmmName(outmatchModel.getOmmName().trim());
		outmatchModelDB.setOmmDesc(outmatchModel.getOmmDesc().trim());
		outmatchModelDB.setOmmRowHeight(outmatchModel.getOmmRowHeight());
		if (outmatchModelDB.getOmmNoTitleMap().containsKey(
				outmatchModel.getOmmNoTitle().toString())
				&& outmatchModelDB.getOmmOutputTypeMap().containsKey(
						outmatchModel.getOmmOutputType())
				&& outmatchModelDB.getOmmStatisticPlaceMap().containsKey(
						outmatchModel.getOmmStatisticPlace().toString())
				&& outmatchModelDB.getOmmStatisticDisplayTypeMap().containsKey(
						outmatchModel.getOmmStatisticDisplayType().toString())) {
			outmatchModelDB.setOmmNoTitle(outmatchModel.getOmmNoTitle());
			outmatchModelDB.setOmmOutputType(outmatchModel.getOmmOutputType());
			outmatchModelDB.setOmmStatisticPlace(outmatchModel
					.getOmmStatisticPlace());
			outmatchModelDB.setOmmStatisticDisplayType(outmatchModel
					.getOmmStatisticDisplayType());
		} else {
			System.out.println("==========omm Maps are illegal");
			return "fail";
		}
		Date nowDate = new Date();
		outmatchModelDB.setOmmLastChangeBy(getCurrentEmpNo());
		outmatchModelDB.setOmmLastChangeTime(nowDate);
		if (outmatchModelDB.getOmmId() == null) {
			outmatchModelDB.setOmmCreateBy(getCurrentEmpNo());
			outmatchModelDB.setOmmCreateTime(nowDate);
			outmatchModelDB.setOmmDefault(Integer.valueOf(0));
		}
		Map omDbMap = new HashMap();
		Outmatch omTmp;
		for (Iterator i$ = outmatchModelDB.getOmList().iterator(); i$.hasNext(); omDbMap
				.put(omTmp.getOmOmb().getOmbId(), omTmp))
			omTmp = (Outmatch) i$.next();

		FormatToString formatToString = new FormatToString();
		for (int i = omList.size() - 1; i >= 0; i--) {
			omTmp = (Outmatch) omList.get(i);
			if (omTmp.getOmIsOutput() == null
					|| omTmp.getOmIsOutput().intValue() != 1) {
				omList.remove(i);
				continue;
			}
			if (omTmp.getOmOmb() == null)
				return "omOmb is null";
			if (omDbMap.containsKey(omTmp.getOmOmb().getOmbId())) {
				omTmp.setOmOmb(((Outmatch) omDbMap.get(omTmp.getOmOmb()
						.getOmbId())).getOmOmb());
				omDbMap.remove(omTmp.getOmOmb().getOmbId());
			} else {
				return "omOmb.ombId does not exist";
			}
			if (omTmp.getOmFieldDesc() == null || omTmp.getOmIsGroup() == null
					|| omTmp.getOmSortId() == null)
				return (new StringBuilder()).append(omTmp.getOmFieldDesc())
						.append(": required parameter is null").toString();
			if (formatToString.setFormat(omTmp.getOmOmb().getOmbDataType(),
					omTmp.getOmFormat()))
				omTmp.setOmFormat(formatToString.getFormat());
			else
				return (new StringBuilder()).append(omTmp.getOmFieldDesc())
						.append(": omFormat is illeagle").toString();
			if (omTmp.getOmStatistic() != null
					&& omTmp.getOmStatistic().length() == 0)
				omTmp.setOmStatistic(null);
			if (omTmp.getOmColumnWidth() != null
					&& omTmp.getOmColumnWidth().intValue() < 1)
				omTmp.setOmColumnWidth(null);
			omTmp.setAllMaps();
			if (!omTmp.getOmIsGroupMap().containsKey(
					omTmp.getOmIsGroup().toString())
					|| omTmp.getOmStatistic() != null
					&& !omTmp.getOmStatisticMap().containsKey(
							omTmp.getOmStatistic()))
				return (new StringBuilder()).append(omTmp.getOmFieldDesc())
						.append(": map parameter is null").toString();
			omTmp.setOmOmm(outmatchModelDB);
		}

		Collections.sort(omList, new BeanComparator(Outmatch.PROP_OM_SORT_ID));
		for (int i = 0; i < omList.size(); i++) {
			Outmatch tmpOutmatch = (Outmatch) omList.get(i);
			tmpOutmatch.setOmSortId(Integer.valueOf(i));
		}

		outmatchModelDB.setOmList(omList);
		List omDelList = new ArrayList();
		Iterator i$ = omDbMap.values().iterator();
		do {
			if (!i$.hasNext())
				break;
			omTmp = (Outmatch) i$.next();
			if (omTmp.getOmId() != null)
				omDelList.add(omTmp);
		} while (true);
		IOutmatchModelBO outmatchModelBO = (IOutmatchModelBO) getBean("outmatchModelBO");
		outmatchModelBO.saveOrUpdateOutmatchModel(outmatchModelDB, omDelList);
		return "success";
	}

	public String setDefaultOutmatchModel(String ioId, String ommId)
			throws Exception {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("iodef", "edit"))) {
			System.out.println("==========error: has no auth");
			return "success";
		}
		IOutmatchModelBO outmatchModelBO = (IOutmatchModelBO) getBean("outmatchModelBO");
		OutmatchModel outmatchModel = outmatchModelBO.getObject(
				new Iodef(ioId), ommId);
		if (outmatchModel == null) {
			System.out.println((new StringBuilder()).append(
					"==========ommId doesn't exist:").append(ommId).toString());
			return "success";
		}
		if (outmatchModel.getOmmDefault().intValue() == 1) {
			System.out.println((new StringBuilder()).append(
					"==========ommId is default:").append(ommId).toString());
			return "success";
		} else {
			OutmatchModel ommDefault = outmatchModelBO
					.loadDefaultObject(new Iodef(ioId));
			List outmatchModels = new ArrayList();
			Date nowDate = new Date();
			ommDefault.setOmmDefault(Integer.valueOf(0));
			ommDefault.setOmmLastChangeBy(getCurrentEmpNo());
			ommDefault.setOmmLastChangeTime(nowDate);
			outmatchModel.setOmmDefault(Integer.valueOf(1));
			outmatchModel.setOmmLastChangeBy(getCurrentEmpNo());
			outmatchModel.setOmmLastChangeTime(nowDate);
			outmatchModels.add(ommDefault);
			outmatchModels.add(outmatchModel);
			outmatchModelBO.saveOrUpdate(outmatchModels);
			return (new StringBuilder()).append(outmatchModel.getOmmId())
					.append(":").append(ommDefault.getOmmId()).toString();
		}
	}

	public String deleteOutmatchModel(String ioId, String ommId)
			throws Exception {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("iodef", "edit"))) {
			System.out.println("==========error: has no auth");
			return "success";
		}
		IOutmatchModelBO outmatchModelBO = (IOutmatchModelBO) getBean("outmatchModelBO");
		OutmatchModel outmatchModel = outmatchModelBO.getObject(
				new Iodef(ioId), ommId);
		if (outmatchModel == null) {
			System.out.println((new StringBuilder()).append(
					"==========ommId doesn't exist:").append(ommId).toString());
			return "success";
		}
		if (outmatchModel.getOmmDefault().intValue() == 1) {
			System.out.println((new StringBuilder()).append(
					"==========ommId is default:").append(ommId).toString());
			return "success";
		} else {
			IOutmatchBO outmatchBO = (IOutmatchBO) getBean("outmatchBO");
			List omList = outmatchBO.getOutmatchList(outmatchModel);
			outmatchModel.setOmList(omList);
			outmatchModelBO.deleteOutmatchModel(outmatchModel);
			return outmatchModel.getOmmId();
		}
	}

	public String checkDataTypeFormat(String dataType, String format,
			Integer trIndex) {
		if (trIndex == null)
			return "error";
		FormatToString formatToString = new FormatToString();
		String result = "";
		if (!formatToString.setFormat(dataType, format))
			result = (new StringBuilder()).append("-").append(
					trIndex.toString()).append(":").toString();
		result = (new StringBuilder()).append(result).append(
				formatToString.getFormat() != null ? formatToString.getFormat()
						: "").toString();
		return result;
	}

	public List getOutmatchModelList(String ioName) {
		IIodefBo iodefBO = (IIodefBo) getBean("iodefbo");
		IOutmatchModelBO outmatchModelBO = (IOutmatchModelBO) getBean("outmatchModelBO");
		Iodef iodef = iodefBO.loadObjectByName(ioName);
		List result = outmatchModelBO.getListByIodef(iodef);
		for (int i = 0; i < result.size(); i++)
			((OutmatchModel) result.get(i)).setOmmIo(iodef);

		return result;
	}

	public void setIoId(String ioId) {
		this.ioId = ioId;
	}

	public String getIoId() {
		return ioId;
	}

	public List getOmmList() {
		return ommList;
	}

	public Iodef getIodef() {
		return iodef;
	}

	private static final long serialVersionUID = 1L;
	private String ioId;
	private List ommList;
	private Iodef iodef;
}
